package ahxu.search.db;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class SearchServiceProvider implements ApplicationContextAware{
private static SearchServiceProvider searchServiceProvider = null;
	
	private static  ApplicationContext classPathXmlApplicationContext = null;
	
	private static String[] contextXml = new String[] {
		"classpath*:resources/*-spring-core.xml",
		"classpath*:resources/search/*-spring-core.xml",
	};

	private SearchServiceProvider() {
		super();
		initContext();
	}
	
	private  SearchServiceProvider(ApplicationContext context) {
		super();
		classPathXmlApplicationContext=context;
	}

	private void initContext() {
		classPathXmlApplicationContext = 
			new ClassPathXmlApplicationContext(contextXml);
	}
	
	public static SearchServiceProvider getInstance() {
		if(searchServiceProvider==null) {
			searchServiceProvider = new SearchServiceProvider();
		}
		return searchServiceProvider;
	}
	public static SearchServiceProvider getInstance(ApplicationContext context) {
		if(searchServiceProvider==null) {
			searchServiceProvider = new SearchServiceProvider(context);
		}
		return searchServiceProvider;
	}
	
	public IFinalResultDao getFinalResultDao(){
		return getCommonBean("finalResultDao",DefaultFinalResultDao.class);
	}
	
	public IUnFinalResultDao getUnFinalResultDao(){
		return getCommonBean("unfinalResultDao",UnFinalResultDao.class);
	}
	
	
	
	
	 @SuppressWarnings("unchecked")
	private <T> T getCommonBean(String name,Class<T> type){
		return (T)classPathXmlApplicationContext.getBean(name);
	}

	public void setApplicationContext(ApplicationContext context) throws BeansException {
		classPathXmlApplicationContext=context;		
	}
}
